using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using luck.Helpers;
using luck.Models;


namespace luck.Controllers
{
   
    // View Model Classes
    public class ManageCompanyFormViewModel
    {
        //Properties
        public luckCompany luckCompany { get; private set; }

        //Contructor
        public ManageCompanyFormViewModel(luckCompany luckcompany)
        {
            luckCompany = luckcompany;
        }
    }

    public class ManageCompanyController : Controller
    {
        luckCompanyRepository luckcompanyrepository = new luckCompanyRepository();
        //
        // GET: /ManageCompany/

        public ActionResult Index()
        {
            return View();
        }
        
        //
        //GET: /ManageCompany/
        public ActionResult List()
        {
            var companys = luckcompanyrepository.FindAllCompanys().ToList();
            return View(companys);
        }
        //
        //POST: /ManageCompany/Search
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Search(FormCollection formValues)
        {
            var companys = luckcompanyrepository.SearchCompanys(formValues.Get("key")).ToList();
            return View(companys);
        }
        //
        // GET: /ManageCompany/Create

        public ActionResult Create()
        {
            luckCompany luckcompany = new luckCompany()
            {
            };
            return View(luckcompany);
        }
        //
        // POST: /ManageCompany/Create
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(luckCompany luckcompany)
        {
            if (ModelState.IsValid)
            {
                
                try
                {
                    luckcompany.DateCreated = DateTime.Now;
                    luckcompany.UserIDCreated = 1;

                    luckcompanyrepository.Add(luckcompany);
                    luckcompanyrepository.Save();

                   // return RedirectToAction("Details", new { id = luckcompany.ID });
                    return RedirectToAction("List");
                }
                catch
                {
                    ModelState.AddModelErrors(luckcompany.GetRuleViolations());
                }
            }
            return View(new ManageCompanyFormViewModel(luckcompany));
        }
        //
        // GET: /ManageCompany/Edit/1
        public ActionResult Edit(int id)
        {
            luckCompany luckcompany = luckcompanyrepository.GetLuckCompany(id);

            return View(luckcompany);
        }
        //
        // POST: /ManageCompany/Edit/1
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            luckCompany luckcompany = luckcompanyrepository.GetLuckCompany(id);
            try
            {
                luckcompany.UserIDModified = 2;
                luckcompany.DateModified = DateTime.Now;
                UpdateModel(luckcompany);
                luckcompanyrepository.Save();
                return RedirectToAction("Details", new { id = luckcompany.ID });
            }
            catch
            {
                ModelState.AddModelErrors(luckcompany.GetRuleViolations());
                return View(new ManageCompanyFormViewModel(luckcompany));
            }
        }
        //
        // GET: /ManageCompany/Details/1
        public ActionResult Details(int id)
        {
            luckCompany luckcompany = luckcompanyrepository.GetLuckCompany(id);
            if (luckcompany == null)
                return View("NotFound");
            else
                return View(luckcompany);
        }
        //
        // GET: /ManageCompany/Delete/1
        public ActionResult Delete(int id)
        {
            luckCompany luckcompany = luckcompanyrepository.GetLuckCompany(id);
            if (luckcompany == null)
                return View("NotFound");
            luckcompanyrepository.Delete(luckcompany);
            luckcompanyrepository.Save();
            return RedirectToAction("List");
        }
    }
}
